<?xml version="1.0" encoding="windows-1250"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xml:space="default">
  <xsl:output method="xml" encoding="windows-1250" indent="yes"/>

  <xsl:template match="/">
    <Genet>
      <xsl:apply-templates select="//GenNode"/>
    </Genet>
  </xsl:template>

  <xsl:template match="GenNode">

    <Gen name="{@name}">
      <xsl:if test="@wraptag and @wraptag!=''">
        <xsl:attribute name="wraptag"><xsl:value-of select="@wraptag"/></xsl:attribute>
      </xsl:if>
      <xsl:attribute name="cachetype"><xsl:value-of select="@cachetype"/></xsl:attribute>
      <xsl:attribute name="onlist"><xsl:value-of select="@onlist"/></xsl:attribute>
      <xsl:attribute name="execfor"><xsl:value-of select="@execfor"/></xsl:attribute>
      <xsl:attribute name="execforevt"><xsl:value-of select="@execforevt"/></xsl:attribute>
      <xsl:attribute name="exectime"><xsl:value-of select="@exectime"/></xsl:attribute>
      <xsl:if test="count(GenNodeTitles/GenNodeTitle)&gt;0">
        <Titles>
          <xsl:for-each select="GenNodeTitles/GenNodeTitle/Title">
            <Title lang="{../@id_lang}"><xsl:value-of select="."/></Title>
          </xsl:for-each>
          <xsl:for-each select="GenNodeTitles/GenNodeTitle/Descr">
            <Descr lang="{../@id_lang}"><xsl:value-of select="."/></Descr>
          </xsl:for-each>
        </Titles>
      </xsl:if>      
      <xsl:if test="count(GenNodeParams/GenNodeParam)&gt;0">
        <Params>
          <xsl:for-each select="GenNodeParams/GenNodeParam">
            <Param code="{@code}" optional="{@optional}">
            <xsl:if test="count(GenNodeParamTitles/GenNodeParamTitle)&gt;0">
              <Titles>
                <xsl:for-each select="GenNodeParamTitles/GenNodeParamTitle/Title">
                  <Title lang="{../@id_lang}"><xsl:value-of select="."/></Title>
                </xsl:for-each>
                <xsl:for-each select="GenNodeParamTitles/GenNodeParamTitle/Descr">
                  <Descr lang="{../@id_lang}"><xsl:value-of select="."/></Descr>
                </xsl:for-each>
              </Titles>
            </xsl:if>      
              <xsl:if test="@default and @default!=''">
                <xsl:attribute name="default"><xsl:value-of select="@default"/></xsl:attribute>
              </xsl:if>
            </Param>
          </xsl:for-each>
        </Params>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="count(GenNodeSources/GenNodeSource)&gt;1">
          <Sources>
            <xsl:apply-templates select="GenNodeSources/GenNodeSource">
              <xsl:sort select="@position" data-type="number" order="ascending"/>
            </xsl:apply-templates>
          </Sources>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="GenNodeSources/GenNodeSource"/>
        </xsl:otherwise>
      </xsl:choose>
      <Target>
        <xsl:if test="tgtname and tgtname!='' and @tgttype!='xda' and @tgttype!='xdadf'">
          <xsl:attribute name="name"><xsl:value-of select="tgtname"/></xsl:attribute>
        </xsl:if>
        <xsl:if test="@tgtencoding and @tgtencoding!=''">
          <xsl:attribute name="encoding"><xsl:value-of select="@tgtencoding"/></xsl:attribute>
        </xsl:if>
        <xsl:if test="tgttemplate and tgttemplate!=''">
          <xsl:attribute name="template"><xsl:value-of select="tgttemplate"/></xsl:attribute>
        </xsl:if>
        <xsl:if test="tgtforeach and tgtforeach!=''">
          <xsl:attribute name="foreach"><xsl:value-of select="tgtforeach"/></xsl:attribute>
        </xsl:if>
        <xsl:if test="@tgttype and @tgttype!=''">
          <xsl:choose>
            <xsl:when test="@tgttype='xda' or @tgttype='xdadf'">
              <xsl:attribute name="type">xml</xsl:attribute>
              <xsl:attribute name="xdaurl">
                <xsl:text/>%BASEURL%/xda/%CONTEXTREP%/%CONTEXTMODEL%/?<xsl:text/>
              </xsl:attribute>
              <xsl:attribute name="xdacv">
                <xsl:value-of select="tgtname"/>
              </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
              <xsl:attribute name="type"><xsl:value-of select="@tgttype"/></xsl:attribute>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
        <xsl:if test="@tgtasparam and @tgtasparam!=''">
          <xsl:attribute name="asparam"><xsl:value-of select="@tgtasparam"/></xsl:attribute>
        </xsl:if>
        <xsl:for-each select="GenNodeTargetParams/GenNodeTargetParam">
          <SendParam code="{@code}">
            <xsl:if test="@default and @default!=''">
              <xsl:attribute name="default">
                <xsl:value-of select="@default"/>
              </xsl:attribute>
            </xsl:if>
            <xsl:if test="@source and @source!=''">
              <xsl:attribute name="source">
                <xsl:value-of select="@source"/>
              </xsl:attribute>
            </xsl:if>
          </SendParam>
        </xsl:for-each>
        <xsl:for-each select="GenNodeFilters/GenNodeFilter">
          <Filter code="{@param}">
            <xsl:if test="@path and @path!=''">
              <xsl:attribute name="source">
                <xsl:value-of select="@path"/>
              </xsl:attribute>
            </xsl:if>
          </Filter>
        </xsl:for-each>
      </Target>
      
    </Gen>

  </xsl:template>

  <xsl:template match="GenNodeSource">
    <Source isdependence="{@isdependence}" type="{@type}">
      <xsl:if test="@foreachparam and @foreachparam!=''">
        <xsl:attribute name="foreachparam"><xsl:value-of select="@foreachparam"/></xsl:attribute>
      </xsl:if>
      <xsl:if test="@ifcondition and @ifcondition!=''">
        <xsl:attribute name="ifcondition"><xsl:value-of select="@ifcondition"/></xsl:attribute>
      </xsl:if>
      <xsl:if test="@pagestep and @pagestep!=''">
        <xsl:attribute name="pagestep"><xsl:value-of select="@pagestep"/></xsl:attribute>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="@type='url'">
          <xsl:attribute name="url"><xsl:value-of select="@datasource"/></xsl:attribute>       
        </xsl:when>
        <xsl:when test="@type='ref'">
          <xsl:attribute name="ref"><xsl:value-of select="@refgennode.name"/></xsl:attribute>       
        </xsl:when>
        <xsl:when test="@type='xda' or @type='xdadf'">       
          <xsl:attribute name="url">
            <xsl:text/>%BASEURL%/xda/%CONTEXTREP%/%CONTEXTMODEL%/?<xsl:text/>
          </xsl:attribute>
          <xsl:variable name="xdacmd">
            <xsl:choose>
              <xsl:when test="@type='xdadf'">Diff</xsl:when>
              <xsl:otherwise>GetData</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <SendParam code="query">
            <xsl:attribute name="default">
              <xsl:text/>&lt;<xsl:value-of select="$xdacmd"/> conceptview="<xsl:value-of select="@datasource"/>"<xsl:if test="@path"> path="<xsl:value-of select="@path"/>"</xsl:if>&gt;<xsl:text/>
              <xsl:text/>&lt;Where type="and"&gt;<xsl:text/>
                <xsl:if test="GenNodeSourceParams/GenNodeSourceParam[@match and @operator]">
                  <xsl:for-each select="GenNodeSourceParams/GenNodeSourceParam[@match and @operator]">
                    <xsl:text/>&lt;<xsl:value-of select="@operator"/> property="<xsl:value-of select="@match"/>" param="<xsl:value-of select="@code"/>"/&gt;<xsl:text/>
                  </xsl:for-each>
                </xsl:if>
                <xsl:if test="SSubConditionGrps/SSubConditionGrp[SSubConditions/SSubCondition]">
                  <xsl:for-each select="SSubConditionGrps/SSubConditionGrp[@type='and']/SSubConditions/SSubCondition[@property and @operator and @default]">    	          
                    <xsl:text/>&lt;Condition operator="<xsl:value-of select="@operator"/>" property="<xsl:value-of select="@property"/>" default="<xsl:value-of select="@default"/>"/&gt;<xsl:text/>
                  </xsl:for-each>    	
                  <xsl:for-each select="SSubConditionGrps/SSubConditionGrp[@type='or' and SSubConditions/SSubCondition[@property and @operator and @default]]">
                    <xsl:text/>&lt;SubCondition type="or"&gt;<xsl:text/>
                      <xsl:for-each select="SSubConditions/SSubCondition[@property and @operator and @default]">
                        &lt;<xsl:value-of select="@operator"/> property="<xsl:value-of select="@property"/>" default="<xsl:value-of select="@default"/>"/&gt;<xsl:text/>
                      </xsl:for-each>
                    <xsl:text/>&lt;/SubCondition&gt;<xsl:text/>
                  </xsl:for-each>       
                </xsl:if>
              <xsl:text/>&lt;/Where&gt;<xsl:text/>
              <xsl:if test="GenNSOrderBys/GenNSOrderBy">
                <xsl:text/>&lt;Orders&gt;<xsl:text/>
                  <xsl:for-each select="GenNSOrderBys/GenNSOrderBy[GenNSOProperties/GenNSOProperty]">
                    <xsl:sort data-type="number" select="@position"/>    	            
                    <xsl:text/>&lt;OrderBy on="<xsl:value-of select="@onelement"/>" type="<xsl:value-of select="@type"/>"&gt;<xsl:text/>
                    <xsl:for-each select="GenNSOProperties/GenNSOProperty">
                      <xsl:sort data-type="number" select="@position"/> 
                        <xsl:text/>&lt;Property name="<xsl:value-of select="@name"/>"/&gt;<xsl:text/>
                      </xsl:for-each>
                    <xsl:text/>&lt;/OrderBy&gt;<xsl:text/>
                  </xsl:for-each>
                <xsl:text/>&lt;/Orders&gt;<xsl:text/>
              </xsl:if>
              <xsl:text/>&lt;/<xsl:value-of select="$xdacmd"/>&gt;<xsl:text/>          
            </xsl:attribute>
          </SendParam>
          
        </xsl:when>
      </xsl:choose>
            
      <xsl:for-each select="GenNodeSourceParams/GenNodeSourceParam">
        <xsl:choose>
          <xsl:when test="@isgetparam='1'">
            <GetParam code="{@code}">
              <xsl:if test="@default and @default!=''">
                <xsl:attribute name="default">
                  <xsl:value-of select="@default"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:if test="@source and @source!=''">
                <xsl:attribute name="source">
                  <xsl:value-of select="@source"/>
                </xsl:attribute>
              </xsl:if>
            </GetParam>
          </xsl:when>
          <xsl:otherwise>
            <SendParam code="{@code}">
              <xsl:if test="@default and @default!=''">
                <xsl:attribute name="default">
                  <xsl:value-of select="@default"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:if test="@source and @source!=''">
                <xsl:attribute name="source">
                  <xsl:value-of select="@source"/>
                </xsl:attribute>
              </xsl:if>
            </SendParam>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Source>
  </xsl:template>

</xsl:stylesheet>